using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.Office.Project.EPMSync.Config.Library
{
    public static class WebServiceUrl
    {
        private const string _admin = "_vti_bin/psi/Admin.asmx";
        private const string _archive = "_vti_bin/psi/Archive.asmx";
        private const string _calendar = "_vti_bin/psi/Calendars.asmx";
        private const string _cubeAdmin = "_vti_bin/psi/CubeAdmin.asmx";
        private const string _customFields = "_vti_bin/psi/customfields.asmx";
        private const string _events = "_vti_bin/psi/Events.asmx";
        private const string _loginForms = "_vti_bin/psi/LoginForms.asmx";
        private const string _loginWindows = "_vti_bin/psi/LoginWindows.asmx";
        private const string _lookupTable = "_vti_bin/psi/lookuptable.asmx";
        private const string _notifications = "_vti_bin/psi/Notifications.asmx";
        private const string _objectLinkProvider = "_vti_bin/psi/ObjectLinkProvider.asmx";
        private const string _project = "_vti_bin/psi/project.asmx";
        private const string _queueSystem = "_vti_bin/psi/queuesystem.asmx";
        private const string _resource = "_vti_bin/psi/Resource.asmx";
        private const string _resourcePlan = "_vti_bin/psi/ResourcePlan.asmx";
        private const string _Security = "_vti_bin/psi/Security.asmx";
        private const string _Statusing = "_vti_bin/psi/Statusing.asmx";
        private const string _TimeSheet = "_vti_bin/psi/TimeSheet.asmx";
        private const string _WssInterop = "_vti_bin/psi/WssInterop.asmx";

        private enum WebService
        {
            Admin, Archive, Calendar, CubeAdmin, CustomFields, Events, LoginForms, LoginWindows, LookupTable,
            Notifications, ObjectLinkProvider, Project, QueueSystem, Resource, ResourcePlan, Security, Statusing, TimeSheet, WssInterop
        }

        public static string Admin(string psInstanceUrl)
        {
            return WsUrl(WebService.Admin, psInstanceUrl);
        }

        public static string Archive(string psInstanceUrl)
        {
            return WsUrl(WebService.Archive, psInstanceUrl);
        }

        public static string Calendar(string psInstanceUrl)
        {
            return WsUrl(WebService.Calendar, psInstanceUrl);
        }

        public static string CubeAdmin(string psInstanceUrl)
        {
            return WsUrl(WebService.CubeAdmin, psInstanceUrl);
        }

        public static string CustomFields(string psInstanceUrl)
        {
            return WsUrl(WebService.CustomFields, psInstanceUrl);
        }

        public static string Events(string psInstanceUrl)
        {
            return WsUrl(WebService.Events, psInstanceUrl);
        }

        public static string LoginForms(string psInstanceUrl)
        {
            return WsUrl(WebService.LoginForms, psInstanceUrl);
        }

        public static string LoginWindows(string psInstanceUrl)
        {
            return WsUrl(WebService.LoginWindows, psInstanceUrl);
        }

        public static string LookupTable(string psInstanceUrl)
        {
            return WsUrl(WebService.LookupTable, psInstanceUrl);
        }

        public static string Notifications(string psInstanceUrl)
        {
            return WsUrl(WebService.Notifications, psInstanceUrl);
        }

        public static string ObjectLinkProvider(string psInstanceUrl)
        {
            return WsUrl(WebService.ObjectLinkProvider, psInstanceUrl);
        }

        public static string Project(string psInstanceUrl)
        {
            return WsUrl(WebService.Project, psInstanceUrl);
        }

        public static string QueueSystem(string psInstanceUrl)
        {
            return WsUrl(WebService.QueueSystem, psInstanceUrl);
        }

        public static string Resource(string psInstanceUrl)
        {
            return WsUrl(WebService.Resource, psInstanceUrl);
        }

        public static string ResourcePlan(string psInstanceUrl)
        {
            return WsUrl(WebService.ResourcePlan, psInstanceUrl);
        }

        public static string Security(string psInstanceUrl)
        {
            return WsUrl(WebService.Security, psInstanceUrl);
        }

        public static string Statusing(string psInstanceUrl)
        {
            return WsUrl(WebService.Statusing, psInstanceUrl);
        }

        public static string TimeSheet(string psInstanceUrl)
        {
            return WsUrl(WebService.TimeSheet, psInstanceUrl);
        }

        public static string WssInterop(string psInstanceUrl)
        {
            return WsUrl(WebService.WssInterop, psInstanceUrl);
        }

        private static string WsUrl(WebService webService, string psInstanceUrl)
        {
            // Test that it's a valid Url
            try
            {
                Uri test = new Uri(psInstanceUrl, UriKind.Absolute);
            }
            catch
            {
                // TODO: maybe we should rethrow the exception, but an exception will thrown on first WS call
                return string.Empty;
            }

            // Fix the end of the Url
            if (!psInstanceUrl.EndsWith("/"))
                psInstanceUrl += "/";

            // Add the project server base instance url to the web service relative url
            switch (webService)
            {
                case WebService.Admin:
                    return psInstanceUrl + _admin;
                case WebService.Archive:
                    return psInstanceUrl + _archive;
                case WebService.Calendar:
                    return psInstanceUrl + _calendar;
                case WebService.CubeAdmin:
                    return psInstanceUrl + _cubeAdmin;
                case WebService.CustomFields:
                    return psInstanceUrl + _customFields;
                case WebService.Events:
                    return psInstanceUrl + _events;
                case WebService.LoginForms:
                    return psInstanceUrl + _loginForms;
                case WebService.LoginWindows:
                    return psInstanceUrl + _loginWindows;
                case WebService.LookupTable:
                    return psInstanceUrl + _lookupTable;
                case WebService.Notifications:
                    return psInstanceUrl + _notifications;
                case WebService.ObjectLinkProvider:
                    return psInstanceUrl + _objectLinkProvider;
                case WebService.Project:
                    return psInstanceUrl + _project;
                case WebService.QueueSystem:
                    return psInstanceUrl + _queueSystem;
                case WebService.Resource:
                    return psInstanceUrl + _resource;
                case WebService.ResourcePlan:
                    return psInstanceUrl + _resourcePlan;
                case WebService.Security:
                    return psInstanceUrl + _Security;
                case WebService.Statusing:
                    return psInstanceUrl + _Statusing;
                case WebService.TimeSheet:
                    return psInstanceUrl + _TimeSheet;
                case WebService.WssInterop:
                    return psInstanceUrl + _WssInterop;
                default:
                    return string.Empty;

            }
        }
    }
}
